
import 'dart:async';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:ycbl/config/route_manager.dart';
import 'package:ycbl/utils/log_util.dart';
import 'package:ycbl/view_model/login_state_view_model.dart';
import 'package:ycbl/widget/image.dart';

class SplashPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {

    var size = MediaQuery.of(context).size;

    LogUtil.d('SplashPage build...');

    return Scaffold(
      body: Stack(
        children: <Widget>[

          // logo 覆盖在启动图下方 因为启动图自带 logo
          Align(
            alignment: Alignment.bottomCenter,
            child: Container(
              margin: EdgeInsets.only(bottom: 60),
              child: LocalImage('splash_logo.png', width: 186.67,),
            ),
          ),

          //启动图（含 logo） 竖直方向铺满
          SizedBox(
            width: size.width,
            height: size.height,
            child: CachedNetworkImage(
              fit: BoxFit.cover,
              imageUrl: 'http://bailitianxia-edus.oss-cn-beijing.aliyuncs.com/2020-02-29/%E7%99%BE%E5%88%A9%E8%AF%BE%E5%A0%82%E3%80%80%E7%99%BE%E5%88%A9%E8%AF%BE%E5%A0%82-APP%E5%90%AF%E5%8A%A8%E9%A1%B5-%E7%A1%AE%E5%AE%9A(1)_1.jpg', //这是一个随机获取图片的网址
            ),
          ),

          //跳过按钮
          Align(
            alignment: Alignment.topRight,
            child: Container(
              margin: EdgeInsets.only(top: 40, right: 20),
              decoration: BoxDecoration(
                border: Border.all(
                  color: Colors.black.withAlpha(100),
                  width: 0.5,
                ),
                borderRadius: BorderRadius.circular(6),
                color: Colors.white70,
              ),
              child: InkWell(
                onTap: (){
                  //下一页
                  nextPage(context);
                },
                child: CounterDownText(),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

//倒计时组件，单独出来只重新刷新这个一块儿，而不至于刷新整个页面
class CounterDownText extends StatefulWidget {
  @override
  _CounterDownTextState createState() => _CounterDownTextState();
}

class _CounterDownTextState extends State<CounterDownText> {

  Timer _timer;
  int _countDownSeconds = 3;

  @override
  void initState() {

    if(_timer == null) {
      _timer = Timer.periodic(
        const Duration(seconds: 1),  //每一秒回调一次
            (timer){
          if(_countDownSeconds < 1) {
            //LogUtil.d('倒计时结束，进入下一页');
            _timer.cancel();
            //进入下一页
            nextPage(context);
          }else {
            _countDownSeconds -= 1;
            setState(() {
              //刷新 UI
            });
          }
        },
      );
    }
    super.initState();
  }

  @override
  void dispose() {
    //LogUtil.d('CountDownText dispose..');
    if(_timer != null && _timer.isActive) {
      //LogUtil.d('取消倒计时');
      _timer.cancel();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: 4, horizontal: 8),
      child: Text(
        '跳过 $_countDownSeconds',
        style: TextStyle(fontSize: 13),
      ),
    );
  }
}


void nextPage(BuildContext context) {

  LogUtil.d('to next page...');

  //进入主页面 并且销毁当前页面
  //Navigator.of(context).pushReplacementNamed(BLRouteName.mainNav);

  //如果不使用结果，要加  false
  var model = Provider.of<LoginStateViewModel>(context, listen: false);
  if(model.isLogin) {
    // 已登录，进入首页，且 pop 当前页面
    Navigator.of(context).pushReplacementNamed(BLRouteName.mainNav);
  }else{
    // 未登录，进入登陆页面，且 pop 当前页面，
    // 参数表示是否为根页面，如果为根页面意味着登录成功后，进入首页，
    // 如果是从其他页面进入的，则是回到登录前的那一页
    Navigator.of(context).pushReplacementNamed(BLRouteName.loginPage, arguments: true);
  }

}